using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FreeLancer.HRB.Core.Web.Controls;
using FreeLancer.HRB.Core;
using FreeLancer.HRB.Domains;
using DevExpress.Web.ASPxClasses;

namespace FreeLancer.HRB.WebApp
{
  public partial class Event : WebBasePage
  {
    private const string Next = "Next";
    private const string Prev = "Prev";

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !IsCallback)
      //if (!IsCallback)
      {
        if (Request.RawUrl.Contains("?id=") && Request.QueryString["id"].Length == 36)
        {
          Guid id = new Guid(Request.QueryString["id"]);
          FreeLancer.HRB.Domains.Events eventData =
            GlobalCache.EventsService.GetByEventIdLanguageId(id, _languageId);

          FreeLancer.HRB.Core.Event evnt = null;
          if (eventData != null)
            evnt = new FreeLancer.HRB.Core.Event(eventData);

          if (evnt != null)
          {
            Response.Clear();
            Response.StatusCode = 301;
            Response.AppendHeader("location", evnt.RelativeLink.ToString());
            Response.End();
          }
          else
          {
            Response.Redirect(Utils.RelativeWebRoot + "Error404.aspx", true);
          }
        }
      }

      if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36)
      {
        InitializePageData();
      }
      else
      {
        Response.Redirect(Utils.RelativeWebRoot + "Error404.aspx", true);
      }
    }

    protected void ASPxCallbackPanel_OnCallback(object sender, CallbackEventArgsBase e)
    {
      ExecuteCommand(e.Parameter);
    }

    private void InitializePageData()
    {
      Guid id = new Guid(Request.QueryString["id"]);
      FreeLancer.HRB.Domains.Events eventData =
        GlobalCache.EventsService.GetByEventIdLanguageId(id, _languageId);

      anchorCategoryName.InnerText = Resources.labels.specialEvents;
      anchorCategoryName.HRef = Utils.RelativeWebRoot + "Events.aspx";
      h2Title.InnerText = eventData.Title;
      spanDateTime.InnerText = eventData.DateCreated.ToString();

      Title = eventData.Title + " - HRB";

      TList<Images> images = GlobalCache.ImagesService.GetByEventIdLanguageId(id, _languageId);
      if(images.Count > 0)
      {
        ASPxImage.ImageUrl = Utils.RelativeWebRoot + "Images/EventImages/" + images[0].Path;
        ASPxLabelDescription.Text = images[0].Description;
        ASPxLabelTitle.Text = images[0].Title;
        ASPxLabelPageIndex.Text = "1";
        ASPxLabelPage.Text = images.Count.ToString();

        ASPxBtnPrevious.Enabled = false;
        if (images.Count == 1)
          ASPxBtnNext.Enabled = false;
      }

      InitializeMoreNewsData(eventData.EventId, 3);
    }

    private void InitializeMoreNewsData(Guid postId, int numberOfNews)
    {
      TList<FreeLancer.HRB.Domains.Events> moreNewsData = GlobalCache.EventsService.
        GetByLanguageId(_languageId);

      int i = 0;
      foreach (FreeLancer.HRB.Domains.Events moreNewsDataItem in moreNewsData)
      {
        HtmlGenericControl divMoreNews = new HtmlGenericControl("div");
        divMoreNews.Attributes.Add("class", "item-news");

        HtmlAnchor anchorMoreNews = new HtmlAnchor();
        anchorMoreNews.Attributes.Add("class", "more-news");
        anchorMoreNews.InnerText = moreNewsDataItem.Title;
        anchorMoreNews.HRef = "~/Event.aspx?id=" + moreNewsDataItem.EventId.ToString();

        divMoreNews.Controls.Add(anchorMoreNews);

        divMoreNewsItems.Controls.Add(divMoreNews);

        i++;

        if (i == numberOfNews)
          break;
      }
    }

    private void ExecuteCommand(string parameter)
    {
      string[] parameters = parameter.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);

      bool flagSuccess = false;

      if (parameters.Length > 1)
      {
        switch (parameters[1])
        {
          case Next:
            flagSuccess = NextImage(parameters[0]);
            break;
          case Prev:
            flagSuccess = PrevImage(parameters[0]);
            break;
        }
      }
    }

    private bool PrevImage(string p)
    {
      ASPxBtnNext.Enabled = true;
      ASPxBtnPrevious.Enabled = true;
      int index = (int.Parse(p) - 1) - 1;

      GetImage(index);

      return true;
    }

    private bool NextImage(string p)
    {
      ASPxBtnNext.Enabled = true;
      ASPxBtnPrevious.Enabled = true;
      int index = (int.Parse(p) - 1) + 1;

      GetImage(index);

      return true;
    }

    private void GetImage(int index)
    {
      Guid id = new Guid(Request.QueryString["id"]);
      TList<Images> images = GlobalCache.ImagesService.GetByEventIdLanguageId(id, _languageId);
      int showIndex = index + 1;
      if (images.Count > 0)
      {
        ASPxImage.ImageUrl = Utils.RelativeWebRoot + "Images/EventImages/" + images[index].Path;
        ASPxImage.ToolTip = images[index].Title;
        ASPxLabelDescription.Text = images[index].Description;
        ASPxLabelTitle.Text = images[index].Title;
        ASPxLabelPageIndex.Text = showIndex.ToString();
        ASPxLabelPage.Text = images.Count.ToString();

        if (index == 0)
          ASPxBtnPrevious.Enabled = false;
        if (images.Count == showIndex)
          ASPxBtnNext.Enabled = false;
      }
    }
  }
}
